/**
 * by zgb
 * @param ori_url
 */

/* http://www.letv.com/ptv/pplay/48402/14.html */
function validateVideoUrl(ori_url){
	var html = download_page(ori_url);
	var matches = html.match(/vid:(\d+),/);
	if(!matches){
		//not found
		return 'NotFoundVideo';
	}
	
	var xml_url = 'http://www.letv.com/v_xml/'+matches[1]+'.xml';
	var xml_str = download_page(xml_url);
    /*    matches = xml_str.match(/<mmsJson><!\[CDATA\[(.*?)\]\]><\/mmsJson>/);
	if(!matches){
		//not found
		return 'NotFoundVideo';
	}
	
    var json_str = matches[1];*/ 
    var json_str = parse_xml((xml_str), 'mmsJson');
	var json = eval('(' + json_str + ')');
   
	if(!json || !json.bean || !json.bean.video){
		//not found
		return 'NotFoundVideo';
    }

	var sb = [];
    var vi = json.bean.video;
    var duration = 0;
    for(v in json.bean.video){
    	json_str = download_page(vi[v].url);
    	if(!json_str) continue;    		
    	var flv = eval('(' + json_str + ')');
    	if(!flv || !flv.location) continue;
        sb.push(flv.location);
        duration = parseInt(duration) + parseInt(vi[v].duration)
    }

    if(sb.length){
        //matches = html.match(/<title>(.*?)<\/title>/);
        matches = html.match(/<title>([\s\S]*?)<\/title>/);
        if(matches){
     	   return 'success_title:' + encodeURIComponent(matches[1])
     	          + '\n video_urls:' + encodeURIComponent(sb)
     	          + '\n seconds:' + Math.round(parseInt(duration));
        }
    }
 	return 'error:empty_path';	
}

function js_get_media_url(ori_url) {
	var html = download_page(ori_url);
	var matches = html.match(/vid:(\d+),/);
	if(!matches){
		//not found
		return '';
	}
	
	var xml_url = 'http://www.letv.com/v_xml/'+matches[1]+'.xml';
	var xml_str = download_page(xml_url);
    /*    matches = xml_str.match(/<mmsJson><!\[CDATA\[(.*?)\]\]><\/mmsJson>/);
	if(!matches){
		//not found
		return '';
	}
	
    var json_str = matches[1];*/ 
    var json_str = parse_xml((xml_str), 'mmsJson');
	var json = eval('(' + json_str + ')');
   
	if(!json || !json.bean || !json.bean.video){
		//not found
		return '';
    }

	var sb = [];
    var vi = json.bean.video;
    for(v in json.bean.video){
    	json_str = download_page(vi[v].url);
    	if(!json_str) continue;    		
    	var flv = eval('(' + json_str + ')');
    	if(!flv || !flv.location) continue;
        sb.push(flv.location);
    }
    return sb;
}